﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data.OleDb;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class editProfile : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            OleDbConnection connection = new OleDbConnection(ConfigurationManager.ConnectionStrings["HotelsConnectionString"].ToString());
            string sql = "SELECT * FROM Profile WHERE TravellerName = @f1";
            OleDbCommand selectProfile = new OleDbCommand(sql, connection);
            OleDbDataReader reader = null;
            selectProfile.CommandType = System.Data.CommandType.Text;
            selectProfile.Parameters.AddWithValue("@f1", User.Identity.Name);
            connection.Open();
            reader = selectProfile.ExecuteReader();
            if (reader.HasRows)
            {
                reader.Read();
                this.txtFirstName.Text = (!System.DBNull.Value.Equals(reader["FirstName"]) ? (string)reader["FirstName"]:"");
                this.txtLastName.Text = (!System.DBNull.Value.Equals(reader["LastName"]) ? (string)reader["LastName"] : "");
                

            }
        }

       
    }

    protected void btnSaveProfile_Click(object sender, EventArgs e)
    {
        string file = string.Empty;
        string fileName = string.Empty;
        Guid guid = Guid.NewGuid();
        string sql = string.Empty;
        OleDbConnection connection = new OleDbConnection(ConfigurationManager.ConnectionStrings["HotelsConnectionString"].ToString());
        OleDbCommand updateProfile = null;

        if (this.flePicture.HasFile)
        {
            fileName = "images/" + guid + ".jpg";
            file = Request.MapPath("images/") + guid + ".jpg";
            this.flePicture.SaveAs(file);
            sql = "UPDATE Profile SET FirstName = @f1, LastName=@f2, Picture=@f3 WHERE TravellerName = @f4";
            updateProfile = new OleDbCommand(sql, connection);
        }
        else
        {
            
            sql = "UPDATE Profile SET FirstName = @f1, LastName=@f2 WHERE TravellerName = @f4";
            updateProfile = new OleDbCommand(sql, connection);
        }

       
        updateProfile.Parameters.AddWithValue("@f1", this.txtFirstName.Text);
        updateProfile.Parameters.AddWithValue("@f2", this.txtLastName.Text);
        if (fileName != string.Empty)
        {
            updateProfile.Parameters.AddWithValue("@f3", fileName);
        }
        updateProfile.Parameters.AddWithValue("@f4", User.Identity.Name);
        updateProfile.CommandType = System.Data.CommandType.Text;
        connection.Open();
        updateProfile.ExecuteNonQuery();
        updateProfile.Dispose();
        connection.Close();
        connection.Dispose();
        Response.Redirect("User_Profile.aspx?TravellerName=" + User.Identity.Name);
    }
}